{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "f2f1554f",
   "metadata": {},
   "outputs": [],
   "source": [
    "INTEGER, PLUS, EOF = 'INTERGER', 'PLUS', 'EOF'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "45fb3e98",
   "metadata": {},
   "outputs": [],
   "source": [
    "class Token:\n",
    "    \"\"\"记号类\n",
    "    \"\"\"\n",
    "    def __init__(self, value_type, value):\n",
    "        self.value_type = value_type # 记号的类型\n",
    "        self.value = value # 记号的值\n",
    "    \n",
    "    def __str__(self):\n",
    "        \"\"\"重写查看记号内容的方法\"\"\"\n",
    "        return f\"Token({self.value_type}, {self.value})\"\n",
    "    \n",
    "    def __repr__(self):\n",
    "        return self.__str__()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "792d0769",
   "metadata": {},
   "outputs": [],
   "source": [
    "class Interpreter:\n",
    "    \"\"\"解释器类\n",
    "    \"\"\"\n",
    "    def __init__(self, text):\n",
    "        self.text = text\n",
    "        self.position = 0\n",
    "        self.current_token = None\n",
    "    \n",
    "    def error(self):\n",
    "        raise Exception('警告：错误的输入内容！')\n",
    "    \n",
    "    def get_next_token(self):\n",
    "        text = self.text\n",
    "        if self.position >= len(text):\n",
    "            return Token(EOF, None)\n",
    "        current_char = text[self.position]\n",
    "        if current_char.isdigit():\n",
    "            token = Token(INTEGER, int(current_char))\n",
    "            self.position+=1\n",
    "            return token\n",
    "        if current_char == '+':\n",
    "            token = Token(PLUS, current_char)\n",
    "            self.position+=1\n",
    "            return token\n",
    "        self.error()\n",
    "    \n",
    "    def eat(self, token_type):\n",
    "        if self.current_token.value_type == token_type and self.current_token.value_type != EOF:\n",
    "            self.current_token = self.get_next_token()\n",
    "        elif self.current_token.value_type == EOF:\n",
    "            pass\n",
    "        else:\n",
    "            self.error()\n",
    "    \n",
    "    def expr(self):\n",
    "        self.current_token = self.get_next_token()\n",
    "        left = self.current_token\n",
    "        self.eat(INTEGER)\n",
    "        \n",
    "        operator = self.current_token\n",
    "        if self.current_token.value_type == PLUS:\n",
    "            self.current_token = self.get_next_token()\n",
    "        else:\n",
    "            self.error()\n",
    "        \n",
    "        right = self.current_token\n",
    "        self.eat(INTEGER)\n",
    "        \n",
    "        self.eat(EOF)\n",
    "        \n",
    "        return left.value + right.value\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "d6bb9be0",
   "metadata": {},
   "outputs": [],
   "source": [
    "def main():\n",
    "    text = \"4+2\"\n",
    "    interpreter = Interpreter(text)\n",
    "    result = interpreter.expr()\n",
    "    print(text, \"=\", result)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "13d63f4e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "4+2 = 6\n"
     ]
    }
   ],
   "source": [
    "main()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
